#include "StdAfx.h"
#include "TinyCCDBMgr.h"
#include "Usermapper.h"
#include "UserGroupMapper.h"

namespace DBLayer
{

	CTinyCCDBMgr::~CTinyCCDBMgr(void)
	{
	}

	void CTinyCCDBMgr::init(int dbtype, std::string & connstr)
	{
		CTinyDBManager::init(dbtype,connstr);

		/*CTinyDBUserMapper* usermapper = new CTinyDBUserMapper();
		pushMapper(DB_USERINFO,usermapper);

		CTinyDBUserGroupMapper* usergroupmapper = new CTinyDBUserGroupMapper();
		pushMapper(DB_USERGROUP_INFO,usergroupmapper);*/

		trunks = new CTrunkMapper();
		trunks->init(dbtype,m_DBInterface);
		extensions = new CExtensionMapper();
		extensions->init(dbtype,m_DBInterface);
		users = new CUserMapper();
		users->init(dbtype,m_DBInterface);
		cards = new CVoiceCardMapper();
		cards->init(dbtype,m_DBInterface);

		//loadDataFromDB();

	}
	bool CTinyCCDBMgr::ProcessReceivedMsg(CTinyMsg& inMsg, CTinyMsg& outMsg)
	{
		bool bRet = false;
		switch(inMsg.getMsgIndex())
		{
		case CLIENTMGR:
			{
				if(inMsg.getMsgItem() == CLIENTLOGON_REQUEST)
				{

					int dpc = inMsg.getDefaultParamCount();
					string mname,mtype,username,password,region;
					inMsg.getDefaultParamByIndex(CLIENTLOGON_MODULENAME,mname);
					inMsg.getDefaultParamByIndex(CLIENTLOGON_MODULETYPE,mtype);
					inMsg.getDefaultParamByIndex(CLIENTLOGON_USERNAME,username);
					inMsg.getDefaultParamByIndex(CLIENTLOGON_PASSWORD,password);
					inMsg.getDefaultParamByIndex(CLIENTLOGON_REGION,region);
					//CTinyDBUserMapper* usermapper = dynamic_cast<CTinyDBUserMapper*>(findMapper(DB_USERINFO));
					//if(users != NULL)
					{
						//process logon 

						std::string ret = users->getPassWordByName(username);
						//std::string group = usermapper->getGroupID(username);

						if(ret == password)
						{
							//logon succ
							outMsg.setMsgIndex(CLIENTMGR);
							outMsg.setMsgItem(CLIENTLOGON_SUCC);
							outMsg.setDefaultParam(CLIENTLOGON_MODULENAME,mname);
							outMsg.setDefaultParam(CLIENTLOGON_MODULETYPE,mtype);
							outMsg.setDefaultParam(CLIENTLOGON_USERNAME,username);
							outMsg.setDefaultParam(CLIENTLOGON_REGION,region);
							bRet = true;
						}
						else
							bRet = false;
					}

					if(bRet == false)
					{
						outMsg.setMsgIndex(CLIENTMGR);
						outMsg.setMsgItem(CLIENTLOGON_ERROR);
						outMsg.setDefaultParam(CLIENTLOGON_MODULENAME,mname);
						outMsg.setDefaultParam(CLIENTLOGON_MODULETYPE,mtype);
						outMsg.setDefaultParam(CLIENTLOGON_USERNAME,username);
						outMsg.setDefaultParam(CLIENTLOGON_REGION,region);
					}
				}//end if CLIENTLOGON_REQUEST
				/*if(inMsg.getMsgItem() == CLIENTLOGON_REQUEST)
				{

				}*/
			}break;
		default:
			break;
		}//end switch MsgIndex
		return bRet;
	}

}

